package com.chushouya.order.dao.repository;


import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Longs;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.order.dao.entity.OrderAddressEntity;
import com.chushouya.order.dao.mapper.OrderAddressEntityMapper;
import com.chushouya.order.dto.admin.orderaddress.OrderAddressQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 订单Repository
 */
@Repository
public class OrderAddressRepository extends BaseRepository<OrderAddressEntityMapper, OrderAddressEntity, Long> {

    public OrderAddressEntity selectByOrderId(Long orderId) {
        final Example example = this.buildExample();
        example.and().andEqualTo("orderId", orderId);
        return this.selectFirstOneByExample(example);
    }

    public List<OrderAddressEntity> selectByOrderIds(List<Long> orderIds) {
        if (Lists.isEmpty(orderIds)) {
            return Lists.newArrayList();
        }
        final Example example = this.buildExample();
        example.and().andIn("orderId", orderIds);
        return this.selectByExample(example);
    }

    public OrderAddressEntity selectBizByOrderId(Long orderId) {
        OrderAddressQuery query = new OrderAddressQuery();
        query.setOrderId(orderId);
        return this.selectOne(query);
    }

    public void updateUserAddressByOrderId(OrderAddressEntity orderAddressEntity) {
        final Example example = this.buildExample();
        example.and().andEqualTo("orderId", orderAddressEntity.getOrderId());
        this.updateBySelective(orderAddressEntity, example);
    }
    public OrderAddressEntity selectOne(OrderAddressQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }

    private Example buildExample(OrderAddressQuery query) {
        Example example = buildExample();
        if (Longs.notNullAndZero(query.getOrderId())) {
            example.and().andEqualTo("orderId", query.getOrderId());
        }
        return example;
    }
}
